package com.android.gufengmall.my;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.ForumAdapter;
import com.android.gufengmall.adapter.ForumCommentAdapter;
import com.android.gufengmall.bean.DiscussBean;
import com.android.gufengmall.bean.ForumBean;
import com.android.gufengmall.bean.UserBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.ForumBeanDao;
import com.android.gufengmall.gen.UserBeanDao;
import com.android.gufengmall.utils.BitMapConversionUtils;
import com.android.gufengmall.utils.TimeUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class ForumDetailsActivity extends BaseActivity {
    private static final String TAG = "ForumDetailsActivity";

    private App mMyApp;
    private DaoSession mDaoSession;
    private ForumBeanDao mForumBeanDao;
    private UserBeanDao mUserBeanDao;

    private ForumBean mForumBean;
    private UserBean mUserBean;
    private List<DiscussBean> mDiscussList;
    private ForumCommentAdapter mForumCommentAdapter;

    private TextView mTvTitle;
    private TextView mTvInfo;
    private ImageView mImgPhoto;
    private TextView mTvUserName;
    private TextView mTvDate;
    private EditText mEdtComment;
    private Button mBtnSend;
    private RecyclerView mRvComment;
    private int mPosition;
    private String mUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum_details);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mBtnSend.setOnClickListener(v -> {
            Log.d(TAG,"mBtnSend-->");
            String comment = mEdtComment.getText().toString().trim();
            if (comment.isEmpty()) {
                showToast("发布内容不能为空");
                return;
            }
            DiscussBean discussBean = new DiscussBean();
            discussBean.setInfo(comment);
            discussBean.setUserName(mUserBean.getUserName());
            discussBean.setUserPhoto(mUserBean.getUserPhoto());
            discussBean.setDate(TimeUtils.getCurrentSysTime());
            mDiscussList.add(discussBean);
            mForumCommentAdapter.notifyDataSetChanged();
            mForumBean.setDiscussList(mDiscussList);
            mForumBeanDao.update(mForumBean);
            showToast("发布成功");
        });
    }

    private void initData() {
        mPosition = getIntent().getExtras().getInt("position");
        mUserName = getIntent().getExtras().getString("userName");

        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mForumBeanDao = mDaoSession.getForumBeanDao();
        mUserBeanDao = mDaoSession.getUserBeanDao();

        mForumBean = mForumBeanDao.loadAll().get(mPosition);

        List<UserBean> list = mUserBeanDao.queryBuilder().where(UserBeanDao.Properties.UserName.eq(mUserName)).list();
        for (UserBean userBean : list) {
            if (userBean.getUserName().equals(mUserName)) {
                mUserBean = userBean;
            }
        }

        mTvTitle.setText(mForumBean.getTitle());
        mTvInfo.setText(mForumBean.getInfo());
        mTvUserName.setText(mForumBean.getUserName());
        mTvDate.setText(mForumBean.getDate());
        Glide.with(this).load(BitMapConversionUtils.stringToBitmap(mForumBean.getUserPhoto()))
                .dontAnimate().error(R.drawable.icon_photo).fitCenter().into(mImgPhoto);

        Log.d(TAG,"initData-->");
        if (mForumBean.getDiscussList() != null) {
            mDiscussList = mForumBean.getDiscussList();
        } else {
            mDiscussList = new ArrayList<>();
        }

        mRvComment.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mForumCommentAdapter = new ForumCommentAdapter(this, mDiscussList);
        mRvComment.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRvComment.setAdapter(mForumCommentAdapter);
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mTvInfo = findViewById(R.id.tv_info);
        mTvUserName = findViewById(R.id.tv_userName);
        mTvDate = findViewById(R.id.tv_date);
        mImgPhoto = findViewById(R.id.img_photo);
        mEdtComment = findViewById(R.id.edt_comment);
        mBtnSend = findViewById(R.id.btn_send);
        mRvComment = findViewById(R.id.rv_comment);
    }
}